ALTER PROCEDURE [dbo].[CPP_EfficacyOtherPlatform]
  @cbPlatformID TINYINT,        -- 登陆平台; 1=IOS; 2=Android; 3=PC; 4=网页; 5=微信小游戏; 6=QQ玩一玩;
  @strUserUin NVARCHAR(66),     -- 微信unionid
  @strNickName NVARCHAR(32),    -- 用户昵称
  @cbGender TINYINT,            -- 用户性别; 0=未知; 1=男性; 2=女性;
  @strClientIP NVARCHAR(15),    -- 连接地址
  @strMachineID NVARCHAR(66),   -- 机器标识
  @strWXImageURL NVARCHAR(200), -- 微信头像URL
  @Channel  INT,                -- RYPlatformDB.dbo.GameChannel.ID
  @szResult NVARCHAR(255) OUTPUT  -- 输出信息
AS
BEGIN

  -- 属性设置
  SET NOCOUNT ON

  -- 基本信息
  DECLARE @UserID INT = 0
  DECLARE @CustomID INT
  DECLARE @FaceID SMALLINT
  DECLARE @Accounts NVARCHAR(31)
  DECLARE @NickName NVARCHAR(31)
  DECLARE @UserUin NVARCHAR(66)
  DECLARE @UnderWrite NVARCHAR(63)
  DECLARE @PlayTimeCount INT
  -- 积分变量
  DECLARE @Score BIGINT = 0     -- 身上金币
  DECLARE @Insure BIGINT = 0    -- 保险箱金币

  -- 扩展信息
  DECLARE @GameID INT = 0
  DECLARE @Gender TINYINT = 0
  DECLARE @UserMedal INT
  DECLARE @Experience INT
  DECLARE @LoveLiness INT
  DECLARE @MemberOrder SMALLINT
  DECLARE @MemberOverDate DATETIME
  DECLARE @MemberSwitchDate DATETIME
  DECLARE @GrantScoreCount BIGINT = 0  -- 新用户注册系统送游戏币的数目

  IF LEN(@strUserUin) = 0 BEGIN
    SET @szResult = N'微信unionid无效'
    RETURN 1
  END

  IF (@cbGender<>0 AND @cbGender<>1 AND @cbGender<>2) BEGIN
    SET @szResult = N'性别无效'
    RETURN 2
  END

  -- 渠道号判断
  DECLARE @State INT = 0

  SELECT @State = State, @GrantScoreCount = ISNULL(RegZengCoin, 0) FROM RYPlatformDB.dbo.GameChannel WHERE ID = @Channel
  IF @@ROWCOUNT=0 BEGIN
    SET @szResult = N'渠道[' + CAST(@Channel AS NVARCHAR) + ']不存在';
    RETURN 3
  END
  IF @State<>1 BEGIN
    SET @szResult = N'渠道[' + CAST(@Channel AS NVARCHAR) + ']未启用';
    RETURN 4
  END

  DECLARE @EnjoinLogon INT = 0

  SELECT @EnjoinLogon=ISNULL(StatusValue, 0) FROM SystemStatusInfo(NOLOCK) WHERE StatusName=N'EnjoinLogon'
  IF @EnjoinLogon <> 0 BEGIN  -- 系统暂停
    SELECT @szResult=StatusString FROM SystemStatusInfo(NOLOCK) WHERE StatusName=N'EnjoinLogon'
    RETURN 5
  END

  -- 效验IP地址
  SELECT @EnjoinLogon=ISNULL(EnjoinLogon, 0) FROM ConfineAddress(NOLOCK) WHERE AddrString=@strClientIP AND GETDATE()<EnjoinOverDate
  IF @EnjoinLogon <> 0 BEGIN
    SET @szResult=N'抱歉地通知您，系统禁止了您所在的 IP 地址的登录功能，请联系客户服务中心了解详细情况！'
    RETURN 6
  END

  -- 效验机器
  SELECT @EnjoinLogon=ISNULL(EnjoinLogon, 0) FROM ConfineMachine(NOLOCK) WHERE MachineSerial=@strMachineID AND GETDATE()<EnjoinOverDate
  IF @EnjoinLogon <> 0 BEGIN
    SET @szResult=N'抱歉地通知您，系统禁止了您的机器的登录功能，请联系客户服务中心了解详细情况！'
    RETURN 7
  END

  DECLARE @DateID INT = CAST(CAST(GETDATE() AS FLOAT) AS INT)

  -- 查询用户
  DECLARE @Nullity TINYINT
  DECLARE @StunDown TINYINT
  DECLARE @InsurePass NCHAR(32) = '' -- 保险箱密码
  DECLARE @MoorMachine TINYINT = 0
  DECLARE @MachineSerial NCHAR(32)
  DECLARE @loginUserRight INT
  DECLARE @oldHeadImageUrl NVARCHAR(256)
  DECLARE @szDynamicPass NVARCHAR(32) = '' -- 动态密码

  SELECT
    @UserID=a.UserID, @GameID=GameID, @Accounts=Accounts, @NickName=NickName,
    @UserUin=UserUin, @UnderWrite=UnderWrite,
    @FaceID=FaceID, @CustomID=CustomID, @Gender=Gender, @Nullity=Nullity,
    @StunDown=StunDown, @UserMedal=UserMedal, @Experience=Experience,
    @InsurePass=InsurePass, @LoveLiness=LoveLiness, @MemberOrder=MemberOrder,
    @MemberOverDate=MemberOverDate, @MemberSwitchDate=MemberSwitchDate,
    @loginUserRight = UserRight, @MoorMachine=MoorMachine, @MachineSerial=LastLogonMachine,
    @PlayTimeCount=PlayTimeCount,
    @oldHeadImageUrl = HeadImgUrl, @szDynamicPass=DynamicPass
  FROM AccountsInfo(NOLOCK) a LEFT JOIN IndividualDatum b ON b.UserID=a.UserID
  WHERE a.AccountsType = 0 AND UserUin = @strUserUin AND Channel = @Channel

  IF @UserID > 0 BEGIN  -- 账号存在, 更新字段
    IF @NickName <> @strNickName BEGIN
      SET @NickName = @strNickName
    END
    IF @cbGender <> @Gender BEGIN
      SET @Gender = @cbGender
    END

    UPDATE AccountsInfo SET
      NickName = @NickName, Gender = @Gender, LastLogonMachine = @strMachineID,
      LastLogonIP = @strClientIP, GameLogonTimes=GameLogonTimes+1, LastLogonDate = GETDATE()
    WHERE UserID = @UserID

    IF @strWXImageURL <> N'' AND @strWXImageURL <> @oldHeadImageUrl BEGIN
      UPDATE IndividualDatum SET HeadImgUrl = @strWXImageURL  WHERE UserID = @UserID
    END

  END ELSE BEGIN  -- 用户不存在, 注册新用户

      -- 分配标识
      SELECT @GameID = ISNULL(Code, 0) FROM RYWebSMSDB.dbo.CodeIdentifier(NOLOCK) WHERE AccountsUsed = 0
      IF @GameID = 0 BEGIN
        SET @szResult = N'系统分配游戏标识失败'
        RETURN 8;
      END

      -- 生成账号
      DECLARE @strTemp NVARCHAR(31) = CONVERT(NVARCHAR(31),REPLACE(NEWID(),'-','_'))
      SET @szDynamicPass = CONVERT(NVARCHAR(32),REPLACE(newid(),'-',''))
      -- 注册用户
      INSERT AccountsInfo (
        Accounts,NickName,RegAccounts,PlatformID,UserUin,LogonPass,InsurePass,Gender,FaceID,
        GameLogonTimes,LastLogonIP,LastLogonMobile,LastLogonMachine,RegisterIP,RegisterMobile,
        RegisterMachine,Channel,GameID, DynamicPass, DynamicPassTime, LastLogonDate, RegisterDate
      ) VALUES (
        @strTemp,@strNickName,@strTemp,@cbPlatformID,@strUserUin,N'd1fd5495e7b727081497cfce780b6456',N'',@cbGender,0,
        1,@strClientIP,N'',@strMachineID,@strClientIP,N'',@strMachineID,@Channel,@GameID, @szDynamicPass, GETDATE(),
        GETDATE(), GETDATE()
      )

      SELECT @UserID = @@IDENTITY;

      UPDATE RYWebSMSDB.dbo.CodeIdentifier SET AccountsUsed = @UserID WHERE Code = @GameID

      -- 查询用户
      SELECT
        @Accounts=Accounts, @NickName=NickName,
        @UserUin=UserUin, @UnderWrite=UnderWrite,
        @FaceID=FaceID, @CustomID=CustomID, @Gender=Gender, @Nullity=Nullity,
        @StunDown=StunDown, @UserMedal=UserMedal, @Experience=Experience,
        @LoveLiness=LoveLiness, @MemberOrder=MemberOrder, @MemberOverDate=MemberOverDate,
        @MemberSwitchDate=MemberSwitchDate, @MoorMachine=MoorMachine,
        @PlayTimeCount=PlayTimeCount, @loginUserRight = UserRight,
        @szDynamicPass=DynamicPass
      FROM AccountsInfo(NOLOCK) WHERE UserID = @UserID

      -- 记录今日注册次数
      UPDATE SystemStreamInfo SET GameRegisterSuccess=GameRegisterSuccess+1 WHERE DateID=@DateID
      IF @@ROWCOUNT=0 BEGIN
        INSERT SystemStreamInfo (DateID, GameRegisterSuccess) VALUES (@DateID, 1)
      END

      -- 附属资料
      INSERT  IndividualDatum(
        UserID,QQ,EMail,SeatPhone,MobilePhone,DwellingPlace,PostalCode,CollectDate,
        UserNote,MoorMobile,nRadius,sRadius,HeadImgUrl,UserName,UserCardID
      )VALUES(@UserID,N'',N'',N'',N'',N'',N'',GetDate(),N'',0,0,0,@strWXImageURL,N'',N'')

      -- 注册赠送金币
      IF @GrantScoreCount>0 BEGIN
        -- 更新/插入/记录
        UPDATE SystemGrantCount SET GrantScore=GrantScore+@GrantScoreCount, GrantCount=GrantCount+1 WHERE DateID=@DateID AND RegisterIP=@strClientIP
        IF @@ROWCOUNT = 0 BEGIN
          INSERT SystemGrantCount (DateID, RegisterIP, RegisterMachine, GrantScore, GrantCount) VALUES (@DateID, @strClientIP, N'', @GrantScoreCount, 1)
        END

        -- 查询金币
        DECLARE @CurrScore BIGINT = 0 -- 身上金币
        DECLARE @CurrInsure BIGINT =0 -- 保险箱金币
        SELECT @CurrScore=ISNULL(Score, 0), @CurrInsure=ISNULL(InsureScore, 0) FROM RYTreasureDBLink.RYTreasureDB.dbo.GameScoreInfo WHERE UserID=@UserID
        IF @@ROWCOUNT = 0 BEGIN
          -- 赠送金币
          INSERT RYTreasureDBLink.RYTreasureDB.dbo.GameScoreInfo (UserID, Score, RegisterIP, LastLogonIP)
          VALUES (@UserID, @GrantScoreCount, @strClientIP, @strClientIP)
        END

        -- 用户流水账
        INSERT INTO RYTreasureDBLink.RYTreasureDB.dbo.RecordPresentInfo(UserID,PreScore,PreInsureScore,PresentScore,TypeID,IPAddress,CollectDate)
        VALUES (@UserID,@CurrScore,@CurrInsure,@GrantScoreCount,1,@strClientIP,GETDATE())

        -- 用户日统计
        UPDATE RYTreasureDBLink.RYTreasureDB.dbo.StreamPresentInfo SET
          DateID=@DateID, PresentCount=PresentCount+1,PresentScore=PresentScore+@GrantScoreCount
        WHERE UserID=@UserID AND TypeID=1

        IF @@ROWCOUNT=0 BEGIN
          INSERT RYTreasureDBLink.RYTreasureDB.dbo.StreamPresentInfo(DateID,UserID,TypeID,PresentCount,PresentScore)
          VALUES(@DateID,@UserID,1,1,@GrantScoreCount)
        END
      END

  END

  -- 帐号禁止
  IF @Nullity<>0 BEGIN
    SET @szResult = N'您的帐号暂时处于冻结状态，请联系客户服务中心了解详细情况！'
    RETURN 9
  END

  -- 帐号关闭
  IF @StunDown<>0 BEGIN
    SET @szResult = N'您的帐号使用了安全关闭功能，必须重新开通后才能继续使用！'
    RETURN 10
  END

  -- 固定机器
  IF @MoorMachine = 1 BEGIN
    IF @MachineSerial<>@strMachineID BEGIN
      SET @szResult = N'您的帐号使用固定机器登录功能，您现所使用的机器不是所指定的机器！'
      RETURN 11
    END
  END

  -- 查询身上金币和保险箱金币
  SELECT @Score = ISNULL(Score, 0), @Insure=ISNULL(InsureScore, 0) FROM RYTreasureDBLink.RYTreasureDB.dbo.GameScoreInfo WHERE UserID = @UserID

  -- 会员等级
  IF @MemberOrder<>0 AND GETDATE()>@MemberSwitchDate BEGIN
    DECLARE @UserRight INT
    SET @UserRight=0

    -- 删除会员
    DELETE AccountsMember WHERE UserID=@UserID AND MemberOverDate<=GETDATE()

    -- 搜索会员
    SELECT @MemberOverDate=MAX(MemberOverDate), @MemberOrder=MAX(MemberOrder), @MemberSwitchDate=MIN(MemberOverDate)
    FROM AccountsMember(NOLOCK) WHERE UserID=@UserID

    -- 数据调整
    IF @MemberOrder IS NULL
    BEGIN
      SET @MemberOrder=0
      SET @UserRight=512
    END
    IF @MemberOverDate IS NULL SET @MemberOverDate='1980-1-1'
    IF @MemberSwitchDate IS NULL SET @MemberSwitchDate='1980-1-1'

    -- 更新数据
    UPDATE AccountsInfo SET
      MemberOrder=@MemberOrder, MemberOverDate=@MemberOverDate, MemberSwitchDate=@MemberSwitchDate,
      UserRight=UserRight&~@UserRight WHERE UserID=@UserID
  END

  --判断玩家是否在房间中，如果在房间中不更新动态密码
  DECLARE @LockServerIDCheck INT = 0
  SELECT @LockServerIDCheck = ISNULL(ServerID, 0) FROM RYTreasureDBLink.RYTreasureDB.dbo.GameScoreLocker WHERE UserID = @UserID
  IF @LockServerIDCheck=0 BEGIN -- 每次登陆都更新动态密码
    SET @szDynamicPass = CONVERT(NVARCHAR(32),REPLACE(newid(),'-',''))
    UPDATE AccountsInfo SET DynamicPassTime=GETDATE(), DynamicPass=@szDynamicPass WHERE UserID=@UserID
  END

  -- 记录日志
  UPDATE SystemStreamInfo SET GameLogonSuccess=GameLogonSuccess+1 WHERE DateID=@DateID
  IF @@ROWCOUNT=0 BEGIN
    INSERT SystemStreamInfo (DateID, GameLogonSuccess) VALUES (@DateID, 1)
  END

  -- 记录日志
  INSERT RYRecordDB.dbo.RecordUserLoginLog(UserID,LoginType,MoorMachine,LoginIP) VALUES (@UserID,@cbPlatformID,@strMachineID,@strClientIP)

  DECLARE @InsureEnabled TINYINT = 0 -- 银行密码为空提示
  IF @InsurePass <> '' BEGIN
    SET @InsureEnabled = 1
  END

  -- 输出变量
  SELECT
    @FaceID AS FaceID, @UserID AS UserID, @GameID AS GameID, @CustomID AS CustomID,
    @Experience AS Experience, @LoveLiness AS LoveLiness, @Accounts AS Accounts,
    @NickName AS NickName, @szDynamicPass AS DynamicPass, @Score AS Score,
    @UserMedal AS Ingot, @Insure AS Insure, @Gender AS Gender,
    @MoorMachine AS MoorMachine, @UnderWrite AS UnderWrite,
    @MemberOrder AS MemberOrder, @MemberOverDate AS MemberOverDate,
    @InsureEnabled AS InsureEnabled,
    @strWXImageURL AS WxImageUrl, @loginUserRight AS UserRight

  RETURN 0
END